/*
 * @file accum.h
 *
 * Functions for accumulation and counting
 *
 * @author Dahua Lin
 */

#ifndef _MSC_VER
#pragma once
#endif

#ifndef GMLPP_ACCUM_H_
#define GMLPP_ACCUM_H_

#include <gmlpp/base/arma.h>
#include <gmlpp/base/range.h>
#include <limits>

namespace gml
{

	/**
	 * counts should be pre-initialized
	 */
	template<typename TInt, typename Range>
	inline void intcount(const Range& rgn, const uword len, const TInt *x, uword *counts)
	{
		for (uword i = 0; i < len; ++i)
		{
			TInt v = x[i];

			if (rgn.contains(v))
			{
				++ counts[rgn.index(v)];
			}
		}
	}


	/**
	 * counts should be pre-initialized
	 */
	template<typename TInt, typename Range>
	inline void intcount(const Range& rgn, const Mat<TInt>& x, Mat<uword>& counts)
	{
		check_arg(counts.n_elem == (uword)rgn.size(),
				"intcount:The size of counts is inconsistent with the range size.");

		intcount(rgn, x.n_elem, x.memptr(), counts.memptr());
	}




}


#endif
